<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: create by allen.qiang
// +----------------------------------------------------------------------

/**
 * 会员发送消息类
 * @package  module/base/customer
 * @author   create by allen.qiang
 * @version 1.0
 */

class custSearchMessage{

    /**
     * 查询会员发送消息
     */
    public function searchcustSearchMessage( &$arg_array ) {

        global $G_SHOP;

        //sql字符串
        $sql = '';

        //SQL字段类型字符串
        $params_type = '';

        //SQL字段值数组
        $params_array = array();

        //会员名
        if (isset($arg_array['custID']) && !empty($arg_array['custID'])) {
            $sql .= ' AND custID = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['custID'];
        }

        //会员消息ID
        if (isset($arg_array['message_id']) && is_numeric($arg_array['message_id'])) {
            $sql .= ' AND message_id = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['message_id'];
        }

        //会员号
        if (isset($arg_array['pcustID']) && !empty($arg_array['pcustID'])) {
            $sql .= ' AND pcustID = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['pcustID'];
        }

        //会员标题
        if (isset($arg_array['message_title']) && !empty($arg_array['message_title'])) {
            $sql .= ' AND message_title = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['message_title'];
        }


        //是否已读
        if (isset($arg_array['is_read']) && !empty($arg_array['is_read'])) {
            $sql .= ' AND  is_read = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['is_read'];
        }

        //是否已回复
        if (isset($arg_array['is_reply']) && !empty($arg_array['is_reply'])) {
            $sql .= ' AND  is_reply = ?';
            $params_type .= 'i';
            $params_array[] = $arg_array['is_reply'];
        }

        //回复时间
        if (isset($arg_array['reply_time']) && !empty($arg_array['reply_time'])) {
            $sql .= ' AND  reply_time = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['reply_time'];
        }

        //回复内容
        if (isset($arg_array['reply_content']) && !empty($arg_array['reply_content'])) {
            $sql .= ' AND  reply_content = ?';
            $params_type .= 's';
            $params_array[] = $arg_array['reply_content'];
        }

        if ($sql) {
            $sql = 'WHERE ' . substr($sql, 4);
        }

        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 1) {

                //统计数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM cust_message ' . $sql;
                $count_array = &$G_SHOP->DBCA->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }else{

                    $arg_array['count'] = $item_count;
                    //分页bug处理
                    if ($arg_array['count'] < $arg_array['rp']) {

                        $arg_array['page'] = 1;
                    }
                    //排序
                    if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                        $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                    //分页
                    if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                        $arg_array['page'] = 1;

                    $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);
                }
            } else {

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }


            //查询分类信息
            $search_sql = 'SELECT * FROM cust_message ' . $sql;
            $temp_item_array = &$G_SHOP->DBCA->stmt_query($search_sql, $params_type, $params_array);
            if (!$temp_item_array)
                return null;


            foreach ($temp_item_array as &$item) {

                //用户信息数组
                $item_array[]  = array(
                    'uuid'	    => 	$item->uuid,
                    'timeStamp'	=>	$item->timeStamp,
                    'creator' 	=> 	$item->creator,
                    'createTime' => $item->createTime,
                    'custID' => $item->custID,
                    'pcustID' => $item->pcustID,
                    'message_id'	=> 	$item->message_id,
                    'message_title'	=> 	$item->message_title,
                    'is_read' => $item->is_read,
                    'is_reply' => $item->is_reply,
                    'reply_time' =>$item->reply_time,
                    'reply_content'	=> 	$item->reply_content
                );
            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;

        }

    }

    /**
     * 删除会员发送消息
     * @param array $pay_array 会员发送消息ID数组
     * @return array
     */
    public function deleteMessageMaster ( $pay_array ) {

        global $G_SHOP;

        if (!isset($pay_array) || !is_array($pay_array) || empty($pay_array)) {
            return array( 'code' => 400, 'message' => '参数错误' );
        }

        try {

            $G_SHOP->DBCA->beginTransaction();

            foreach($pay_array as &$message_id) {

                //查询会员发送消息方式
                $pay_obj = new entity('cust_message');
                $pay_obj->message_id = $message_id;

                if (!$G_SHOP->DBCA->fetch($pay_obj)) {
                    throw new Exception('会员发送消息信息没有找到', 404);
                }

                //删除会员发送消息信息
                $G_SHOP->DBCA->delete($pay_obj);

            }

            $G_SHOP->DBCA->commit();

            return array( 'code' => 200, 'message' => '删除会员发送消息成功' );

        } catch ( Exception $e ) {

            $G_SHOP->DBCA->rollBack();

            if ($e->getCode() > 0) {
                return array( 'code' => $e->getCode(), 'message' => $e->getMessage() );
            } else {
                error_log($e->getMessage());
                return array( 'code' => 500, 'message' => '系统错误' );
            }

        }

    }


}